

package org.easy.wechatpay;

import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.core.notification.RequestParam;
import com.wechat.pay.java.service.payments.app.AppService;
import com.wechat.pay.java.service.payments.h5.H5Service;
import com.wechat.pay.java.service.payments.h5.model.Amount;
import com.wechat.pay.java.service.payments.h5.model.PrepayRequest;
import com.wechat.pay.java.service.payments.h5.model.PrepayResponse;
import com.wechat.pay.java.service.payments.h5.model.QueryOrderByOutTradeNoRequest;
import com.wechat.pay.java.service.payments.model.Transaction;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;


/**
 * 微信支付服务.
 */
public class WechatPayTemplate {

    private final WechatPayProperties properties;

    private final H5Service h5Service;
    private final AppService appService;
    private final NotificationParser parser;

    public static Map<String, String> successMap = new HashMap<>();

    static {
        successMap.put("code", "SUCCESS");
        successMap.put("message", "成功");
    }

    public static Map<String, String> failMap = new HashMap<>();

    static {
        failMap.put("code", "FAIL");
        failMap.put("message", "失败");
    }


    public WechatPayTemplate(RSAAutoCertificateConfig rsaAutoCertificateConfig,
                             WechatPayProperties properties) {
        this.properties = properties;
//        this.h5Service = new H5Service.Builder().config(rsaAutoCertificateConfig).build();
        this.h5Service = null;
//        this.parser = new NotificationParser(rsaAutoCertificateConfig);
        this.parser = null;
        this.appService = null;
    }

    public Transaction parseWithCheck(HttpServletRequest request, String body) {
        String signature = request.getHeader("Wechatpay-Signature");
        // 请求头Wechatpay-nonce
        String nonce = request.getHeader("Wechatpay-Nonce");
        // 请求头Wechatpay-Timestamp
        String timestamp = request.getHeader("Wechatpay-Timestamp");
        // 微信支付证书序列号
        String serial = request.getHeader("Wechatpay-Serial");
        // 签名方式
        String signType = request.getHeader("Wechatpay-Signature-Type");
        // 构造 RequestParam
        RequestParam requestParam = new RequestParam.Builder()
                .serialNumber(serial)
                .nonce(nonce)
                .signature(signature)
                .timestamp(timestamp)
                .signType(signType)
                .body(body)
                .build();
        return parser.parse(requestParam, Transaction.class);
    }

    public String appPay(String outTradeNo, String description, BigDecimal totalFee) {
        com.wechat.pay.java.service.payments.app.model.PrepayRequest prepayRequest = new com.wechat.pay.java.service.payments.app.model.PrepayRequest();
        prepayRequest.setDescription(description);
        prepayRequest.setOutTradeNo(outTradeNo);
        prepayRequest.setNotifyUrl(properties.getNotifyUrl());
        com.wechat.pay.java.service.payments.app.model.Amount amount = new com.wechat.pay.java.service.payments.app.model.Amount();
        // 微信支付的单位是分，这里都需要乘以100
        amount.setTotal(totalFee.multiply(new BigDecimal("100")).intValue());
        prepayRequest.setAmount(amount);

        com.wechat.pay.java.service.payments.app.model.PrepayResponse prepay1 = appService.prepay(prepayRequest);
        return prepay1.getPrepayId();
    }

    public String h5Pay(String outTradeNo, String description, BigDecimal totalFee) {
        PrepayRequest request = new PrepayRequest();
        request.setDescription(description);
        request.setOutTradeNo(outTradeNo);
        request.setNotifyUrl(properties.getNotifyUrl());
        Amount amount = new Amount();
        // 微信支付的单位是分，这里都需要乘以100
        amount.setTotal(totalFee.multiply(new BigDecimal("100")).intValue());
        request.setAmount(amount);

        PrepayResponse prepay = h5Service.prepay(request);
        return prepay.getH5Url();
    }

    public Transaction h5Query(String outTradeNo) {
        QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
        request.setOutTradeNo(outTradeNo);
        request.setMchid(properties.getMchId());

        return h5Service.queryOrderByOutTradeNo(request);
    }


}
